﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using VioletDevelopmentLaboratory.Net.Web;

public partial class vboard_webmanager_user_modify : VioletDevelopmentLaboratory.Net.Web.PageBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Roles.Enabled)
		{
			UserRoleManagerPanel.Visible = false;
		}

		if (!Page.IsPostBack)
		{
			User_DataBind();
			Roles_DataBind();
			BackHyperLink.NavigateUrl = Request.UrlReferrer.AbsoluteUri;
		}
	}

	private MembershipUser getMembershipUser()
	{
		Guid providerUserKey = new System.Guid(Request.QueryString["userid"]);

		return Membership.GetUser(providerUserKey);
	}

	private void User_DataBind()
	{
		MembershipUser user = getMembershipUser();

		UserNameLiteral.Text = user.UserName;
		EmailTextBox.Text = user.Email;
		CommentTextBox.Text = user.Comment;
		PasswordQuestionLiteral.Text = user.PasswordQuestion;
		IsApprovedCheckBox.Checked = user.IsApproved;
		IsLockedOutCheckBox.Checked = user.IsLockedOut;
		IsOnlineCheckBox.Checked = user.IsOnline;
		CreationDateLiteral.Text = user.CreationDate.ToString();
		LastActivityDateLiteral.Text = user.LastActivityDate.ToString();
		LastLoginDateLiteral.Text = user.LastLoginDate.ToString();
		LastLockoutDateLiteral.Text = user.LastLockoutDate.ToString();
		LastPasswordChangedDateLiteral.Text = user.LastPasswordChangedDate.ToString();
	}
	protected void ModifyButton_Click(object sender, EventArgs e)
	{
		MembershipUser user = getMembershipUser();

		string oldEmail = user.Email;

		user.Email = EmailTextBox.Text;
		user.IsApproved = IsApprovedCheckBox.Checked;
		user.Comment = CommentTextBox.Text;

		string resultMessage = "업데이트 완료.";

		try
		{
			Membership.UpdateUser(user);
		}
		catch (System.Configuration.Provider.ProviderException ex)
		{
			resultMessage = ex.Message;
			EmailTextBox.Text = oldEmail;
		}

		VUtility.ShowMessagePanel(Page, resultMessage, true);
	}









	private void Roles_DataBind()
	{
		if (Roles.Enabled)
		{
			RolesCheckBoxList.DataSource = Roles.GetAllRoles();
			RolesCheckBoxList.DataBind();

			MembershipUser user = getMembershipUser();

			string[] userRoles = Roles.GetRolesForUser(user.UserName);

			foreach (string userRole in userRoles)
			{
				RolesCheckBoxList.Items.FindByValue(userRole).Selected = true;
			}
		}
	}
	protected void RolesCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
	{
		MembershipUser user = getMembershipUser();

		foreach (ListItem item in RolesCheckBoxList.Items)
		{
			if (item.Selected)
			{
				if(!Roles.IsUserInRole(user.UserName, item.Value))
					Roles.AddUserToRole(user.UserName, item.Value);
			}
			else
			{
				if(Roles.IsUserInRole(user.UserName, item.Value))
					Roles.RemoveUserFromRole(user.UserName, item.Value);
			}
		}
	}
}
